<?php
/* 
 * FST Extension
 * Abe Yang <abeyang@cal.berkeley.edu> (c) 2008
 * http://code.google.com/p/flickrsetsbytag/
 *
/* ---------------------------------------------------------------------------------- */

require('fst.class.php');

class FSTExtension extends FST {
	function FSTExtension($cache_dir = FST_CACHE_DIR) {
		parent :: FST($cache_dir);
	} // end FSTExtension()

	/* Updates */
	
	// 1. grab all-list from cache
	// 2. update x number of albums based on all-list
	// PRECONDITION: updateAllAlbumsList() must have been executed shortly before running this fn
	function updateSomeAlbums($limit, $offset = 0, $info = 'updateSomeAlbums') {
		$this->getAllAlbumsList($info);
		
		$size = sizeof($this->allalbums);
		// if $limit == 0, update to the end of list
		if (!$limit) {
			$end = $size;
		}
		else {
			$end = $limit + $offset;
			if ($end > $size) $end = $size;
		}
		
		$number_items = $end - $offset;
		$desc = '#items=' . $number_items . ' start=' . $offset;
		$this->log('BEGIN', $info, $desc);
		
		$timer = 0;
		// update each album (within bounds)
		for ($i=$offset; $i<$end; $i++) {
			$album = $this->allalbums[$i];
			$this->updateAlbum($album['id'], 'updateAlbum[' . $i .']' );
			$timer += $this->time_elapsed;
			if (!$this->is_cron && $timer > FST_MAXTIME) break;
		}
		
		$desc = 'total time = ' . $timer . ' sec';
		$this->log('END', $info, $desc);
	}	// end updateSomeAlbums()
	
	// 1. grab all-list from flickr (but do not overwrite cached all-list)
	// 2. update x many recent albums based on new all-list
	// 3. save info of x albums and update cached all-list
	function updateSomeAlbumsAndList($limit = 5, $info = 'updateSomeAlbumsAndList') {
		$desc = '#recent_items=' . $limit;
		$this->log('BEGIN', $info, $desc);

		// fetch from flickr first
		$data = $this->f->photosets_getList();
		// get all-list
		$this->getAllAlbumsList($info);
		// initialize vars
		$albums = array();
		$i = 0;
		$timer = 0;
		
		// cycle through flickrset's albums
		foreach($data['photoset'] as $album) {
			// safety mechanism: if $limit==0, it does NOT go thru entire album list; break immediately.
			if ($i == $limit) break;
			
			// update album itself and save it in cache
			$album = $this->updateAlbum($album['id'], 'updateAlbum[' . $i .']');
			$timer += $this->time_elapsed;
			// check to see if album already exists in all-list
			// must do this (complete) search for each flickr album
			$j = 0;
			$does_album_exist = false;
			$strfound = 'exists';
			unset($album['photo']);		// 'photo' not needed in all-list
			foreach ($this->allalbums as $a) {
				// album exists in all-list: update directly in all-list
				if ($a['id'] == $album['id']) {
					$does_album_exist = true;
					$this->allalbums[$j] = $album;
					break;
				}

				$j++;
			} // end foreach(all-list)
			
			if (!$does_album_exist) {
				// album does not exist in all-list: keep track of this album
				$albums[] = $album;
				$strfound = 'does not exist';
			}
			
			$this->log("\t\t", $info, 'album ' . $strfound . ' in all-list');
			$i++;
		} // end foreach(flickr-album)
		
		$this->startTimer();
		// update all-list (by appending it to $albums)
		$albums = array_merge($albums, $this->allalbums);
				
		$this->save($albums, FST_KEYWORD, $info);
		$timer += $this->time_elapsed;
		
		$desc = 'total time = ' . $timer . ' sec';
		$this->log('END', $info, $desc);
	}	// end updateSomeAlbumsAndList()
	
	function updateAlbumAndList($albumid, $info = 'updateAlbumAndList') {
		$album = $this->updateAlbum($albumid, $info);
		if (!$album) return false;
		
		// get all-list
		$this->getAllAlbumsList($info);
		// initialize vars
		$albums = array();
		$strfound = 'exists';
		$does_album_exist = false;
		$j = 0;
		unset($album['photo']);		// 'photo' not needed in all-list
		
		foreach ($this->allalbums as $a) {
			// album exists in all-list: update directly in all-list
			if ($a['id'] == $albumid) {
				$does_album_exist = true;
				$this->allalbums[$j] = $album;
				break;
			}

			$j++;
		} // end foreach(all-list)
		
		if (!$does_album_exist) {
			// album does not exist in all-list: keep track of this album
			$albums[] = $album;
			$strfound = 'does not exist';
		}
		
		// why am i merging this? commenting out for now (until i figure out)...
		// $albums = array_merge($album, $this->allalbums);
		$this->save($albums, FST_KEYWORD, $info);
		$this->log("\t\t", $info, 'album ' . $strfound . ' in all-list');
		return $albums;
	}	// end updateAlbumAndList()
	

} // end FSTExtension
?>

